{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "848a17be",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入余额：30\n",
      "--------------商品列表--------------\n",
      "商品序号 (0): 蓝莓奶酪贝果 12元\n",
      "商品序号 (1): 抹茶贝果 10元\n",
      "商品序号 (2): 花生贝果 12元\n",
      "商品序号 (3): 干酪 8元\n",
      "请输入商品编号：0\n",
      "商品购买成功，正在制作中...您的余额还有18元\n",
      "--------------商品列表--------------\n",
      "商品序号 (0): 蓝莓奶酪贝果 12元\n",
      "商品序号 (1): 抹茶贝果 10元\n",
      "商品序号 (2): 花生贝果 12元\n",
      "商品序号 (3): 干酪 8元\n",
      "请输入商品编号：2\n",
      "商品购买成功，正在制作中...您的余额还有6元\n",
      "--------------商品列表--------------\n",
      "商品序号 (0): 蓝莓奶酪贝果 12元\n",
      "商品序号 (1): 抹茶贝果 10元\n",
      "商品序号 (2): 花生贝果 12元\n",
      "商品序号 (3): 干酪 8元\n",
      "请输入商品编号：3\n",
      "您的余额不足，请更换支付方式~~\n",
      "--------------商品列表--------------\n",
      "商品序号 (0): 蓝莓奶酪贝果 12元\n",
      "商品序号 (1): 抹茶贝果 10元\n",
      "商品序号 (2): 花生贝果 12元\n",
      "商品序号 (3): 干酪 8元\n"
     ]
    }
   ],
   "source": [
    "product_list = [\n",
    "   [ \"蓝莓奶酪贝果\",12],\n",
    "   [\"抹茶贝果\",10],\n",
    "   [\"花生贝果\",12],\n",
    "   [\"干酪\",8]\n",
    "]\n",
    "shopping_car = []\n",
    "\n",
    "Alipay = int(input(\"请输入余额：\"))\n",
    "\n",
    "while True:\n",
    "    print(\"--------------商品列表--------------\")\n",
    "    for i in range(4):\n",
    "        print(\"商品序号\",\"(\"+str(i)+\"):\" ,product_list[i][0],str(product_list[i][1])+\"元\")\n",
    "\n",
    "    # 2.让用户输入商品编号进行购买：直接用商品列表的index作为商品编号\n",
    "    user_choice = input(\"请输入商品编号：\")\n",
    "    # 情景一：用户正在购买，输入商品编号\n",
    "    if user_choice.isdigit():\n",
    "        # 情景一：余额大于商品价格\n",
    "        if Alipay >= product_list[int(user_choice)][1]:\n",
    "            Alipay = Alipay - product_list[int(user_choice)][1]\n",
    "            shopping_car.append(product_list[int(user_choice)])\n",
    "            print(\"商品购买成功，正在制作中...您的余额还有%s元\"%(Alipay))\n",
    "       # 情景二：余额小于商品价格\n",
    "        else:\n",
    "            print(\"您的余额不足，请更换支付方式~~\")\n",
    "    # 情景二：用户输入退出，结束购买\n",
    "    elif user_choice == \"q\":\n",
    "        print(\"您已购买的商品:\",shopping_car)\n",
    "        print(\"您的余额还有%s,欢迎下次光临~~~\"%(Alipay))\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2f5be08f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
